package com.example.houduan.Controller;

import com.example.houduan.Pojo.Material;
import com.example.houduan.Service.MaterialService;
import com.example.houduan.Utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@CrossOrigin
public class MaterialController {

    @Autowired
    MaterialService materialService;

    @RequestMapping("/getMaterialPage")
    @ResponseBody
    public PageModel getMaterialPage(@RequestParam int num, int size) {
        return materialService.getMaterialPage(num, size);
    }

    @RequestMapping("/addMaterial")
    @ResponseBody
    public String addMaterial(@RequestBody Material material) {
        material.setCreate_time(LocalDateTime.now());
        material.setUpdate_time(LocalDateTime.now());
        if (materialService.addMaterial(material) == 0) {
            return "添加失败";
        } else {
            return "添加成功";
        }
    }

    @RequestMapping("deleteMaterial")
    @ResponseBody
    public String deleteMaterial(Integer meterial_id) {
        if (materialService.deleteMaterial(meterial_id) == 0) {
            return "删除失败";
        } else {
            return "删除成功";
        }
    }

    @RequestMapping("updateMaterial")
    @ResponseBody
    public String updateMaterial(@RequestBody Material material) {
        material.setUpdate_time(LocalDateTime.now());
        if (materialService.updateMaterial(material) == 0) {
            return "修改失败";
        } else {
            return "修改成功";
        }
    }

    @RequestMapping("/getMaterialByConditions")
    @ResponseBody
    public List<Material> getMaterialByConditions(@RequestParam(required = false) Integer meterial_id, String title, String policy_title) {
        return materialService.getMaterialByConditions(meterial_id, title, policy_title);
    }
} 